package net.huashitong.Indicators.service;

import com.sirdc.modules.sys.entity.SysScheduleJob;
import com.sirdc.modules.sys.filter.SysScheduleJobFilter;
import com.sirdc.modules.sys.service.RedisService;
import com.sirdc.modules.sys.service.SysScheduleJobService;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Indicators.dao.ScheduleJobDao;
import net.huashitong.Indicators.entity.BA01;
import net.huashitong.Indicators.entity.BA02;
import net.huashitong.Indicators.filter.BA01Filter;
import net.huashitong.until.Until;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class JobService {

    @Autowired
    private AB01Service ab01Service;

    @Autowired
    private BA01Service ba01Service;

    @Autowired
    private RedisService redisService;

    @Autowired
    private AB02Service studentService;
    @Autowired
    private BA02Service ba02Service;

    @Autowired
    private SysScheduleJobService scheduleJobService;
    @Autowired
    private ScheJobService scheJobService;

    private  static Logger logger  = LoggerFactory.getLogger(JobService.class);
    /**
     * 第二阶段开始，账户减少1，
     * 账户减少1
     */

    public void updateAccount()
    {
        //第二阶段开始，账户减少1

        ab01Service.updateAccount();
        /*SysScheduleJobFilter filter  = new SysScheduleJobFilter();
        filter.setJobName("JobService");
        List<SysScheduleJob> list  = scheJobService.getJobs(filter);

        //System.out.println(list);


        //检测第二阶段是否结束
        String stopDate = redisService.get("overdate");
        SimpleDateFormat sdf  = new SimpleDateFormat("yyyymmdd");
        Date stoptime =null,currentTime=null;

        String currentDate = Until.getCurrentDate();
        try {
            stoptime = sdf.parse(stopDate);
            currentTime= sdf.parse(currentDate);

            int temp = currentDate.compareTo(stopDate);  //
            if(temp>=0){
                //停止账户余额的值
                scheduleJobService.updateStatus2Pause(list.get(0).getSysId());
                redisService.set("choosestatue","0");   //选课状态  0 不可选
                //先选出所有的课程和课程名额，
                List<BA01>  ba01s =ba01Service.getAllCourse(new BA01Filter());
                for(BA01 item:ba01s)
                {// 筛选出结果
                    updateCourseResulet(item.getBAB001().trim(),item.getBAB003().trim());//筛选选课结果
                }
                //已经筛选出结果
                redisService.set("resultCode","1");  //已经有结果 1 ，没有结果0
                String courseDate =Until.getHalfYear(Until.getCurrentDate());
                BA02 ba02 = ba02Service.getDate(courseDate);
                ba02.setBAB204("1");
                ba02Service.update(ba02);
                //开启每天一次
              *//*  filter.setJobName("checkEvery");
                list  = scheJobService.getJobs(filter);
                scheduleJobService.updateStatus2Resume(list.get(0).getSysId());*//*


            }
        } catch (ParseException e) {
            e.printStackTrace();
        }catch (SchedulerException e)
        {
            e.printStackTrace();
        }*/

    }


   // public void updateS





    //每天晚上0点跑一次 检查是检查选课开始结束时间
    public void updateCheckStatue()
    {


        String courseDate =Until.getHalfYear(Until.getCurrentDate());
        BA02 ba02 = ba02Service.getDate(courseDate);
        if(ObjectUtils.isBlank(ba02))
        {
            ba02 = new BA02();
            ba02.setBAB201(Until.getCurrenYear()+"0901");
            ba02.setBAB202(Until.getCurrenYear()+"0907");
            ba02.setBAB203(Until.getCurrenYear()+"0904");
            ba02.setBAB205(courseDate);
            ba02.setBAB204("0");
            ba02Service.save(ba02);
            redisService.set("resultCode","0");
        }

        String starttime = redisService.get("startdate");
        String currenttine = Until.getCurrentDate();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyymmdd");
        Date startDate = null,currentDate =null;
        try {
            startDate= sdf.parse(starttime);
            currentDate=sdf.parse(currenttine);

            String statue  = redisService.get("choosestatue");  //选课状态
            String resultCode  = redisService.get("resultCode");//结果状态
            if(StringUtils.isBlank(resultCode))
            {
                redisService.set("resultCode",ba02.getBAB204());
            }


                if(currentDate.compareTo(startDate)>=0)
                {  //选课开始
                    if("0".equals(statue))
                    {
                      /* BA02 ba02 = ba02Service.getDate();
                         ba02.setBAB204("1");*/
                        redisService.set("choosestatue","1");

                    }
                    if("0".equals(resultCode))   //0 未有选课结果，可以进入第二阶段
                    {

                        //检查第二阶段是否开始
                        String secondTime = redisService.get("seconddate");
                        Date secondDate = sdf.parse(secondTime);

                        if(currentDate.compareTo(secondDate)>=0) //Date前面返回小于0，后面大于0，。
                        {
                            SysScheduleJobFilter filter  = new SysScheduleJobFilter();
                            filter.setJobName("JobService");
                            List<SysScheduleJob> list  = scheJobService.getJobs(filter);
                            scheduleJobService.updateStatus2Resume(list.get(0).getSysId());//开启每分钟一次
                            //关闭每天一次 0点
                            filter.setJobName("checkEvery");
                            list  = scheJobService.getJobs(filter);
                            scheduleJobService.updateStatus2Pause(list.get(0).getSysId()); //关闭每天一次 0点

                        }
                    }
                }


        } catch (ParseException e) {
            e.printStackTrace();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }

       /* //检查第二阶段是否开始
        String secondTime = redisService.get("seconddate");
        try {
            Date secondDate = sdf.parse(secondTime);

            if(currentDate.compareTo(secondDate)<=0) //Date前面， 返回小鱼0，后面大于0，。
            {
                SysScheduleJobFilter filter  = new SysScheduleJobFilter();  //开启每分钟一次
                filter.setJobName("JobService");
                List<SysScheduleJob> list  = dao.queryJobs(filter);
                scheduleJobService.updateStatus2Resume(list.get(0).getSysId());

                //关闭每天一次 0点
                filter.setJobName("checkEvery");
                list  = dao.queryJobs(filter);
                scheduleJobService.updateStatus2Pause(list.get(0).getSysId());

            }


        } catch (ParseException e) {
            e.printStackTrace();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
*/

    }


}
